package com.polimi.meteocal.business.control;

import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.polimi.meteocal.business.entity.Event;
import com.polimi.meteocal.business.entity.Invite;
import com.polimi.meteocal.business.entity.User;

/**
 * Session Bean implementation class InviteManager
 */
@Stateless
@LocalBean
public class InviteManager {
	@Resource
	private EJBContext _context;
	
	@PersistenceContext
	private EntityManager em;
	
	//*** Get the logged user mail
	public String getLoggedUserEmail() {
		return this._context.getCallerPrincipal().getName();
	}
	
	//***Get the Invite list of Logged User
	public List<Invite> getUserInviteList(){
    	List<Invite> userInvites = this.em
				.createNamedQuery(Invite.FIND_FOR_USER, Invite.class)
				.setParameter("user", this.getLoggedUserEmail()).getResultList();
    	return userInvites;
    }
    /**
     * Default constructor. 
     */
    public InviteManager() {}
    
    /**
     *  Store Invites    
     */
    public void saveInvites(List<String> usersToInvite, Event event){
    	//Invites
    	if(usersToInvite!=null){
    		User loggedUsr = this.em
    				.createNamedQuery(User.FIND_FOR_MAIL, User.class)
    				.setParameter("email", getLoggedUserEmail()).getSingleResult();
	    	for(String usr : usersToInvite){
	    		Invite invite=new Invite();
	    		User receiver = this.em
					.createNamedQuery(User.FIND_FOR_MAIL, User.class)
					.setParameter("email", usr).getSingleResult();
	    		invite.setEvent(event);
	    		invite.setUser1(loggedUsr);
	    		invite.setUser2(receiver);
	    		this.em.merge(invite);
	    		this.em.flush();
	    	}
    	}
    }    
    
    public void addInvites(List<String>userToInvite,Event event){
    	User loggeduser = this.em
    			.createNamedQuery(User.FIND_FOR_MAIL , User.class)
				.setParameter("email", getLoggedUserEmail()).getSingleResult();
    	
    	Invite invite;
    	for(String usr : userToInvite){
    		User user = this.em
        			.createNamedQuery(User.FIND_FOR_MAIL , User.class)
    				.setParameter("email", usr).getSingleResult();
    		invite=new Invite();
    		invite.setEvent(event);
    		invite.setUser1(loggeduser);
    		invite.setUser2(user);        	
        	this.em.merge(invite);
        	this.em.flush();
    	}    	
    }
    /**
     * Get the list of unread invites     
     */
    public List<Invite>getUnreadInvites(String user){
    	List<Invite> unreadInvites= this.em
				.createNamedQuery(Invite.LOAD_UNREAD_INVITES, Invite.class)
				.setParameter("user", user).getResultList();
    	return unreadInvites;
    }
    /**
     * Set the mark for Invite, so it's possible to know if the invite is read by user     
     */
    public void setInviteReadMark(List<Invite> readInvites){
    	for(Invite i:readInvites){
    		this.em.merge(i);
    		this.em.flush();
    	}
    }   
}
